package com.example.detect.Activity

import android.content.Context
import android.graphics.Point
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import com.example.dingdingtools.R

class FloatingView @JvmOverloads constructor(
    mContext: Context,
    attrs: AttributeSet? = null
) : LinearLayout(mContext, attrs) {
    private val mWindowManager: WindowManager =
        mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    var mTvPackageName: TextView? = null

    private fun initView() {
        mTvPackageName = findViewById<View>(R.id.tv_package_name) as TextView
    }

    var preP: Point? = null
    var curP: Point? = null
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> preP =
                Point(event.rawX.toInt(), event.rawY.toInt())
            MotionEvent.ACTION_MOVE -> {
                curP = Point(event.rawX.toInt(), event.rawY.toInt())
                val dx = curP!!.x - preP!!.x
                val dy = curP!!.y - preP!!.y
                val layoutParams =
                    this.layoutParams as WindowManager.LayoutParams
                layoutParams.x += dx
                layoutParams.y += dy
                mWindowManager.updateViewLayout(this, layoutParams)
                preP = curP
            }
        }
        return false
    }

    init {
        LayoutInflater.from(mContext).inflate(R.layout.floating_layout, this)
        initView()
    }

}